﻿using System.Text.RegularExpressions;

namespace TomNet.Attributes
{
    /// <summary>
    /// 约束符合正则表达式
    /// </summary>
    public class RegexAttribute : BaseAttribute
    {
        public override string Error
        {
            get => base.Error ?? "正则验证失败";
            set => base.Error = value;
        }
        public string RegexText { get; set; }
        public override bool Validate(object value)
        {
            var regex = new Regex(RegexText);
            // null 数据不验证
            return value == null || regex.Match(value.ToString()).Success;
        }
    }
}
